package handle;

import msg.CreateMsg;
import data.Node;
import data.Tree;

public class CreateMsgHandler {
	private static Tree tree = new Tree();

	public static void hander(CreateMsg msg) throws Exception {
		String path = new String(msg.getPath());
		int index = path.lastIndexOf("/");
		String pPath = path.substring(0, index);
		String name = path.substring(index + 1);
		String value = new String(msg.getValue());

		Node node = tree.get(path);
		if (node != null) {
			throw new Exception();
		}

		node = new Node(path, name, value);

		Node pNode = tree.get(pPath);
		if (pNode == null) {
			throw new Exception();
		}

		node.setpNode(pNode);
		tree.create(node);

		pNode.getSubNodes().add(node);
	}

	public static void main(String[] args) throws Exception {
		/*
		 * MsgHandler.hander((new CreatePathMsg("/root/a", "是世界上最流行的脚本")).encode());
		 * MsgHandler.hander((new CreatePathMsg("/root/a/b", "是世界上最流行的脚本")).encode());
		 * MsgHandler.hander((new CreatePathMsg("/root/a/b/c", "是世界上最流行的脚本")).encode());
		 * MsgHandler.hander((new CreatePathMsg("/root/a/b/c/d", "是世界上最流行的脚本")).encode());
		 * MsgHandler.hander((new CreatePathMsg("/root/a/b/c/d/e", "是世界上最流行的脚本")).encode());
		 * MsgHandler.hander((new CreatePathMsg("/root/a/b/c", "是世界上最流行的脚本")).encode());
		 * MsgHandler.hander((new CreatePathMsg("/root/a/b/c/d", "是世界上最流行的脚本")).encode());
		 * tree.snap();
		 */
		tree.init();
		MsgHandler.hander((new CreateMsg("/root/a/b/c/d", "是世界上最流行的脚本")).encode());

		System.out.println();
	}
}